package com.hgyc.mom.eam.controller;

import java.util.*;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.eam.entity.EamEquipmentType;
import com.hgyc.mom.eam.vo.EamEquipmentTypeParam;
import com.hgyc.mom.eam.service.EamEquipmentTypeService;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 设备类型Controller
 *
 * @author fwj
 * @since 2025-08-10
 */
@Tag(name = "设备类型控制器", description = "设备类型控制器")
@RestController
@RequestMapping("/eam/equipment/type")
public class EamEquipmentTypeController {
    @Autowired
    private EamEquipmentTypeService eamEquipmentTypeService;

    @Operation(summary = "查询设备类型列表", description = "查询设备类型列表")
    @PostMapping("/pageList")
    public Result<IPage<EamEquipmentType>> pageList(@Parameter(description = "查询参数") @RequestBody EamEquipmentTypeParam eamEquipmentTypeParam) {
        Page<EamEquipmentType> page = new Page<>(eamEquipmentTypeParam.getPage(), eamEquipmentTypeParam.getPageSize());
        EamEquipmentType eamEquipmentType = new EamEquipmentType();
        BeanUtils.copyProperties(eamEquipmentTypeParam, eamEquipmentType);
        QueryWrapper<EamEquipmentType> wrapper = QueryWrapperUtil.build(eamEquipmentType);
        IPage<EamEquipmentType> list = eamEquipmentTypeService.page(page, wrapper);
        return Result.success(list);
    }

    @Operation(summary = "下拉设备类型列表", description = "下拉设备类型列表")
    @GetMapping("/selectList")
    public Result<List<EamEquipmentType>> selectList() {
        return Result.success(eamEquipmentTypeService.list());
    }

    @Operation(summary = "获取设备类型详细信息", description = "获取设备类型详细信息")
    @GetMapping(value = "/{id}")
    public Result<EamEquipmentType> getInfo(@Parameter(description = "设备类型ID") @PathVariable("id") Long id) {
        return Result.success(eamEquipmentTypeService.getById(id));
    }
    @Operation(summary = "新增设备类型", description = "新增设备类型")
    @PostMapping
    public Result<?> save(@Parameter(description = "设备类型") @RequestBody EamEquipmentType eamEquipmentType) {
        eamEquipmentTypeService.saveType(eamEquipmentType);
        return Result.success();
    }

    @Operation(summary = "修改设备类型", description = "修改设备类型")
    @PutMapping
    public Result<?> update(@Parameter(description = "设备类型") @RequestBody EamEquipmentType eamEquipmentType) {
        eamEquipmentTypeService.updateType(eamEquipmentType);
        return Result.success();
    }

    @Operation(summary = "删除设备类型", description = "删除设备类型")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        return Result.success(eamEquipmentTypeService.removeByIds(Arrays.asList(ids)));
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<EamEquipmentType> util = new ExcelUtil<>(EamEquipmentType.class);
        util.importTemplateExcel(response, "设备类型");
    }

    @Operation(summary = "导出设备类型列表", description = "导出设备类型列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, EamEquipmentTypeParam eamEquipmentTypeParam) {
        EamEquipmentType eamEquipmentType = new EamEquipmentType();
        BeanUtils.copyProperties(eamEquipmentTypeParam, eamEquipmentType);
        QueryWrapper<EamEquipmentType> wrapper = QueryWrapperUtil.build(eamEquipmentType);
        List<EamEquipmentType> list = eamEquipmentTypeService.list(wrapper);
        ExcelUtil<EamEquipmentType> util = new ExcelUtil<>(EamEquipmentType.class);
        util.exportExcel(response, list, "设备类型数据");
    }

    @Operation(summary = "导入设备类型列表", description = "导入设备类型列表")
    @PostMapping("/import")
    public Result<?> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<EamEquipmentType> util = new ExcelUtil<>(EamEquipmentType.class);
        List<EamEquipmentType> eamEquipmentTypeList = util.importExcel(file.getInputStream());
        String message = eamEquipmentTypeService.importData(eamEquipmentTypeList, updateSupport);
        return Result.success(message);
    }
}